Let me introduce you to Matplotlib, Python's most popular plotting package.
If you have used or seen Matlab before, you'll notice it has a similar style. It produces high quality 2d plots (and 3D extensions), and can make just about any visualization you'd like. Also, many people have created wrappers for this software that makes it iteractive and dynamic (via D3.js).
In [6]:
from matplotlib.pyplot import *
Now, IPython Notebook is not a plot editor, but it can render an image of a plot inline with the notebook. This is useful for when you want to want to quickly view your plot or share data in the notebook with others. You can snapshot your figures and plot them inline using the following command.
In [5]:
%matplotlib inline
(This is known as a cell magic and is an IPython feature... not part of Python. It's simply an internal command for IPython telling it to make render matplotlib plots inside the notebook, not using an external GUI.)
We'll use matplotlib's plot command to make a simple plot of $y=x^{2}$.
Run the following cell and watch it plot!
In [2]:
x = arange(0,10,1) # Create a range of values for x
y = x**2 # evaluate y from x
plot(x,y) # simple plot command from matplotlib. very intuitive
Notice, the ** operator is Python's character for raising something to a power.
The arange command created a range of numbers (as an array) using 3 input values, 0 = starting position, 10 = stoping position, and 1 = step size.
In [ ]:
In [73]:
stem(x,y)
Out[73]:
In [ ]:
In [74]:
import math
from numpy import *
In [76]:
theta = tan(array(y, dtype=float)/array(x))
r = sqrt(array(x)**2 + array(y)**2)
polar(theta,r,'-')
Out[76]:
In [77]:
from IPython.html import widgets
In [167]:
interact = widgets.interact
@interact
def polar_n(n=(0,20)):
x = arange(0,n,.1)
y = x**1.5
theta = tan(array(y, dtype=float)/array(x))
r = sqrt(array(x)**2 + array(y)**2)
polar(theta,r,'.')
In [173]:
@interact
def polynomial(n=(1,7)):
x = arange(-5,5,.1)
y = x
for i in range(n):
y = y + x**(i+1)
plot(x,y, '-')
axis([-5,5,-100,100])
In [ ]: